package com.passioneffect.components {
	
	/**
	 * @author Passion Effect
	 */
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;

	import com.passioneffect.adapter.ContentMovieClipAdapter;
	import com.passioneffect.collection.Iterator;
	import com.passioneffect.display.Component;
	import com.passioneffect.layout.FlowEffectLayout;
	import com.passioneffect.layout.Layout;
	import com.passioneffect.layoutflow.SimpleLayoutFlow;
	import com.passioneffect.parser.ListXMLParser;
	import com.passioneffect.parser.ValuePairParser;

	import Number;

	/*
	 * Events:
	 * Event.OPEN  //select
	 * Event.CLOSE  //unselect
	 * 
	 */

	public class FlowEffect extends Component {
		private var mc : MovieClip;
		protected var flowLayout : Layout;
		public var scrollPercent : Boolean = false;
		protected var _scrollType : String = "fastScroll";

		public function FlowEffect(parentComponent : Component = null) {
			super(parentComponent);
		}

		override protected function preset() : void {
			_direction = "horizontal";
			_layout = new FlowEffectLayout(this);
			
			flowLayout = FlowEffectLayout(_layout);
			startUpMotion = true;
			
			//layoutFlow = new ListLayoutFlow();
			layoutFlow = new SimpleLayoutFlow();
			xmlParser = new ListXMLParser();
			styleXMLParser = new ValuePairParser();
		}

		protected function autoPlayHandler(event : Event) : void {
			//override
		}

		private function fastScrollHandler(evt : Event) : void {
			
			var mx : Number = mouseX;
			var my : Number = mouseY;
			
			var p : Number;
			if(type == "horizontal") {
				p = mx / compWidth;
			}else {
				p = my / compHeight;
			}
			
			scrollXToPercent(p);
		}

		private function followMouseHandler(event : Event) : void {

			var cx : Number = compWidth / 2;
			var cy : Number = compHeight / 2;
			
			var mx : Number = mouseX;
			var my : Number = mouseY;
			
			var sx : Number = _layout.scrollValue.x;
			
			var dx : Number = mx - cx;
			var dy : Number = my - cy;
			var l : Number;
			
			if(type == "horizontal") {
				if(Math.abs(dx) > 5) {
					l = sx + dx * 0.3;
					if(l < -100) {
						l = -100;
					}else if(l > layout.getTotalLengthX() - compWidth) {
						l = layout.getTotalLengthX() - compWidth;
					}
					
					scrollToLength(l);
				}
			}else {
				if(Math.abs(dy) > 5) {
					scrollToLength(sx + dy * 0.3);
				}
			}
		}
		
		override public function set type(t : String) : void {
			if(_direction == t)return;
			_direction = t;
			if(!inited)return;
			
			lockInvalidate = true;
			layoutChangePreset(t);
			
			dispatchChangeSetting("type", t);
			changeMotionInvalidate();
		}
		
		public function removeConstraint(adapter : ContentMovieClipAdapter) : void {
			removeLayoutValue(adapter);
		}

		/*
		 * invalidation when resize component
		 */

		override protected function xmlProgress(event : ProgressEvent) : void {
			//override
		}

		override protected function xmlLoadError(event : IOErrorEvent) : void {
			//override
		}

		override protected function resetData() : void {
			//override
		}
		
		override public function getMinX() : Number {
			var itr : Iterator = items.iterator();
			var min : Number;
			var mc : MovieClip;
			
			if(type == "horizontal") {
				min = itr.next().getMovieClip().x;
				while(itr.hasNext()) {
					mc = itr.next().getMovieClip();
					if(min > mc.x) {
						min = mc.x;
					}
				}
			}else {
				min = itr.next().getMovieClip().y;
				while(itr.hasNext()) {
					mc = itr.next().getMovieClip();
					if(min > mc.y) {
						min = mc.y;
					}
				}
			}
			return min;
		}

		override public function getMaxX() : Number {
			var itr : Iterator = items.iterator();
			var mc : MovieClip;
			var ymax : Number;
			var max : Number ;
			
			if(type == "horizontal") {
				max = ContentMovieClipAdapter(itr.next()).getBounds().right;
				while(itr.hasNext()) {
					ymax = ContentMovieClipAdapter(itr.next()).getBounds().right;
					if(max < ymax) {
						max = ymax;
					}
				}
			}else {
				max = ContentMovieClipAdapter(itr.next()).getBounds().bottom;
				while(itr.hasNext()) {
					ymax = ContentMovieClipAdapter(itr.next()).getBounds().bottom;
					if(max < ymax) {
						max = ymax;
					}
				}
			}
			
			return max;
		}

		override public function getMinY() : Number {
			var itr : Iterator = items.iterator();
			var min : Number;
			var mc : MovieClip;
			
			if(type == "horizontal") {
				min = itr.next().getMovieClip().y;
				while(itr.hasNext()) {
					mc = itr.next().getMovieClip();
					if(min > mc.y) {
						min = mc.y;
					}
				}
			}else {
				min = itr.next().getMovieClip().x;
				while(itr.hasNext()) {
					mc = itr.next().getMovieClip();
					if(min > mc.x) {
						min = mc.x;
					}
				}
			}

			return min;
		}

		override public function getMaxY() : Number {
			var itr : Iterator = items.iterator();
			var mc : MovieClip;
			var ymax : Number;
			var max : Number ;
			
			if(type == "horizontal") {
				max = ContentMovieClipAdapter(itr.next()).getBounds().bottom;
				while(itr.hasNext()) {
					ymax = ContentMovieClipAdapter(itr.next()).getBounds().bottom;
					if(max < ymax) {
						max = ymax;
					}
				}
			}else {
				max = ContentMovieClipAdapter(itr.next()).getBounds().right;
				while(itr.hasNext()) {
					ymax = ContentMovieClipAdapter(itr.next()).getBounds().right;
					if(max < ymax) {
						max = ymax;
					}
				}
			}
			
			return max;
		}

		public function get scrollType() : String {
			return _scrollType;
		}

		public function set scrollType(scrollType : String) : void {
			_scrollType = scrollType;
			if(!_inited)return;
			
			removeMouseEvents();
			setMouseEvents();
		}
	}
}
